using GLib;
using Gtk;
using Gst;


namespace Rob
{
	namespace UI
	{
		public class AnswerCallWidget : Rob.UI.ActionWidget
		{
			private bool playing;
			private bool recording;
			private Rob.Actions.AnswerCall _action;
			private Gtk.Button rec_button;
			private Gtk.Button play_button;
			private Gtk.Button stop_button;
			private Gtk.Box main_box;
			//private Gst.Element pipeline;
			private Gst.Element player;
			private Gst.Pipeline recorder;
			
			

			construct
			{
				
			}
			public Rob.Actions.Action action
			{
				get{ return (Rob.Actions.Action) this._action; }
				set{ this.action = (Rob.Actions.AnswerCall) value; }
			}
			private void create_widgets()
			{
				this.rec_button = new Gtk.Button.from_stock( STOCK_MEDIA_RECORD );
				this.play_button = new Gtk.Button.from_stock( STOCK_MEDIA_PLAY );
				this.stop_button = new Gtk.Button.from_stock( STOCK_MEDIA_STOP );
				this.main_box = new Gtk.HBox( false, 5 );
				this.add_with_properties( this.main_box, "expand", false );
				this.main_box.pack_start_defaults(this.stop_button );
				this.main_box.pack_start_defaults(this.rec_button);
				this.main_box.pack_start_defaults(this.play_button );
				this.stop_button.sensitive = false;
			}

			private void connect_signals()
			{
				this.rec_button.clicked += btn =>
				{
					start();
					this.recording = true;

					//this.stop_button.clicked += btn 
				};
				this.play_button.clicked += btn =>
				{
					start();
					this.playing = true;
				};
				this.stop_button.clicked += btn =>
				{
					stop();

					if( this.playing == true )
						stop_playing();

					else if( this.recording == true )
						stop_recording();
					else
						GLib.critical("Something went wron with playback/recording");
				};

			}
			private void stop()
			{
				this.play_button.sensitive = false;
				this.rec_button.sensitive = false;
				this.stop_button.sensitive = true;
				this.main_box.show_all();

			}
			private void start()
			{
				this.play_button.sensitive = false;
				this.rec_button.sensitive = false;
				this.stop_button.sensitive = true;
				this.main_box.show_all();

			}
			private void stop_playing()
			{
				this.player.set_state(Gst.State.READY);
			}

			private void stop_recording()
			{
				this.recorder.set_state( Gst.State.READY );

			}
			private void start_playing()
			{
				this.player.set_state( Gst.State.PLAYING );

			}

			private void start_recording()
			{
				this.recorder.set_state( Gst.State.PLAYING );

			}
			private void create_elements()
			{

				string filename = this._action.record_file;
				this.player = Gst.ElementFactory.make("playbin2", "player" );
				this.player.set("uri", filename );
				
				//gst-launch -v alsasrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg


				Gst.Element mic = Gst.ElementFactory.make("alsasrc", "micsrc" );
				mic.set("device", "Mic2 Capture Volume");
				Gst.Element converter = Gst.ElementFactory.make("audioconvert", "converter" );
				Gst.Element encoder = Gst.ElementFactory.make( "wavenc", "encoder" );
				Gst.Element file = Gst.ElementFactory.make( "filesink", "file" );
				file.set( "location", filename );
				this.recorder = new Gst.Pipeline("recorder");
				this.recorder.add_many( mic, converter, encoder, file );
				this.recorder.link_many( mic, converter, encoder, file );
				
			}
		}
	}
}
